import 'package:flutter/material.dart';

class FormPage extends StatefulWidget {
  const FormPage({super.key});

  @override
  State<FormPage> createState() => _FormPageState();
}

class _FormPageState extends State<FormPage> {
  final _formKey = GlobalKey<FormState>();
  final _nameCtrl = TextEditingController();
  final _emailCtrl = TextEditingController();
  final _msgCtrl = TextEditingController();
  String? _category;
  bool _agree = false;
  bool _submitting = false;

  @override
  void dispose() {
    _nameCtrl.dispose();
    _emailCtrl.dispose();
    _msgCtrl.dispose();
    super.dispose();
  }

  Future<void> _submit() async {
    if (!_formKey.currentState!.validate()) return;
    setState(() => _submitting = true);
    await Future.delayed(const Duration(seconds: 1));
    setState(() => _submitting = false);
    if (!mounted) return;
    ScaffoldMessenger.of(context).showSnackBar(const SnackBar(content: Text('提交成功')));
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(title: const Text('反馈/资料')),
      body: Padding(
        padding: const EdgeInsets.all(16),
        child: Form(
          key: _formKey,
          child: ListView(
            children: [
              TextFormField(
                controller: _nameCtrl,
                decoration: const InputDecoration(labelText: '姓名'),
                validator: (v) => (v == null || v.trim().isEmpty) ? '请输入姓名' : null,
              ),
              const SizedBox(height: 12),
              TextFormField(
                controller: _emailCtrl,
                decoration: const InputDecoration(labelText: '邮箱'),
                keyboardType: TextInputType.emailAddress,
                validator: (v) {
                  if (v == null || v.isEmpty) return '请输入邮箱';
                  final ok = RegExp(r'^[^@\s]+@[^@\s]+\.[^@\s]+$').hasMatch(v);
                  return ok ? null : '邮箱格式不正确';
                },
              ),
              const SizedBox(height: 12),
              DropdownButtonFormField<String>(
                value: _category,
                items: const [
                  DropdownMenuItem(value: '建议', child: Text('建议')),
                  DropdownMenuItem(value: '问题', child: Text('问题')),
                  DropdownMenuItem(value: '其他', child: Text('其他')),
                ],
                decoration: const InputDecoration(labelText: '类型'),
                onChanged: (v) => setState(() => _category = v),
                validator: (v) => v == null ? '请选择类型' : null,
              ),
              const SizedBox(height: 12),
              TextFormField(
                controller: _msgCtrl,
                maxLines: 4,
                decoration: const InputDecoration(labelText: '内容'),
                validator: (v) => (v == null || v.trim().length < 10) ? '请至少输入 10 个字' : null,
              ),
              const SizedBox(height: 12),
              SwitchListTile(
                title: const Text('同意隐私条款'),
                value: _agree,
                onChanged: (v) => setState(() => _agree = v),
              ),
              const SizedBox(height: 24),
              ElevatedButton.icon(
                onPressed: (_agree && !_submitting) ? _submit : null,
                icon: const Icon(Icons.send),
                label: _submitting ? const Text('提交中...') : const Text('提交'),
              )
            ],
          ),
        ),
      ),
    );
  }
}